package xyz.xiezc.spider.prase;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import xyz.xiezc.ioc.annotation.Component;
import xyz.xiezc.spider.entity.MzituBean;
import xyz.xiezc.spider.common.Request;
import xyz.xiezc.spider.common.Response;
import xyz.xiezc.spider.common.XBean;
import xyz.xiezc.spider.common.XUtil;

import java.util.Objects;

/**
 * @author wb-xzc291800
 * @date 2019/04/18 17:34
 */
@Slf4j
@Component
public class AllHtmlParseImpl implements ParseResponse {

    String urlAll = "https://www.mzitu.com/all/";

    @Override
    public XBean parse(XBean xBean)  {
        Request request = xBean.getRequest();
        String url = request.getUrl();
        Response response = xBean.getResponse();
        if (Objects.equals(urlAll, url)) {
            Document document = XUtil.getDocument(response);
            Elements select = document.select("p.url > a");
            select.forEach(element -> {
                Elements a = element.getElementsByTag("a");
                String href = a.attr("href");
                String text = a.text();

                Request request1 = new Request();
                request1.setUrl(XUtil.getComplieUrl(href, request));
                MzituBean mzituBean = new MzituBean();
                mzituBean.setFetchUrl(href);
                mzituBean.setTitle(text);
                request1.addExtra(request.getExtra());
                request1.addExtra("title",text);
                xBean.addNextRequest(request1);
                xBean.addBean(mzituBean);
            });
            xBean.setStatus(3);
        }
        return xBean;
    }

}
